<template>
  <div>
    <div class="container-box">
      <div class="tableName">
        <p>通知公告</p>
      </div>
      <div class="table-search">
        <div class="input-list">
          <div class="input-box">
            <label for="">标题:</label><input type="text" placeholder="请输入" v-model="title">
          </div>
          <div class="input-box">
            <label for="">创建人:</label><input type="text" placeholder="请输入" v-model="createPeople">
          </div>
          <div class="input-box">
            <label for="">创建时间:</label><input type="date" placeholder="请输入" v-model="createTime">
          </div>
          <div class="input-box">
            <label for="">状态:</label>
            <select name="" id="" class="sel-all" v-model="status">
              <option value="请输入" class="sel-option">请输入</option>
              <option value="待发布" class="sel-option">待发布</option>
              <option value="已发布">已发布</option>
            </select>
          </div>
        </div>
        <div class="search-btn-box">
          <div class="search-btn">
            <button style="margin-right:10px;" @click=check()>查询</button>
            <button @click="reset()">重置</button>
          </div>


        </div>
      </div>
      <div class="notice-list">
        <div class="new">
          <div class="new-notice" @click="addnotice">新增通知公告</div>
        </div>
        <div class="notice-list-box">
          <el-table :data="list" style="width: 100%; color:13px;" border>
            <el-table-column label="序号" min-width="10%" style="color:#333;font-size:13px;">
              <template #default="{ $index }">
                {{ (currentPage - 1) * pageSize + $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column label="标题" min-width="30%" style="color:#333;font-size:13px;">
              <template #default="scope">
                <div style="display: flex; align-items: center;justify-content:center;">
                  <span style="color:#797979; font-size:13px;text-align:center;">{{ scope.row.noticeTitle }}</span>
                </div>
              </template>
            </el-table-column> <el-table-column label="创建人" min-width="10%" height='' style="color:#333;font-size:13px;">
              <template #default="scope">
                <div style="display: flex; align-items: center;justify-content:center;">
                  <span style="color:#797979; font-size:13px;text-align:center;">{{ scope.row.createPeople }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="创建日期" min-width="20%" style="color:#333;font-size:13px;">
              <template #default="scope">
                <div style="display: flex; align-items: center;justify-content:center;">
                  <span style="color:#797979; font-size:13px;text-align:center;">{{ parseTime(scope.row.createDate,
                    "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="状态" min-width="10%" style="color:#333;font-size:13px;">
              <template #default="scope">
                <div style="display: flex; align-items: center;justify-content:center;">
                  <span style="color:#797979; font-size:13px;text-align:center;">{{
                    scope.row.status == 1 ? '发布' : '未发布' }}</span>
                </div>
              </template>
            </el-table-column>

            <el-table-column label="操作" min-width="20%">
              <template #default='scope'>
                <div style="display: flex; align-items: center;justify-content:center;">
                  <div v-for="(item, index) in scope.row.play " :key="index"><span
                      style="color:#049eff; margin-right:5px; font-size:13px;cursor:pointer;"
                      @click="operate(item, scope.row)">{{
                        item }}</span></div>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="table-footer">
            <div class="demo-pagination-block">
              <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[6, 10, 15]"
                :small="small" :disabled="disabled" :background="background"
                layout="total,  prev, pager, next,sizes, jumper" :total="total" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" />
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, onMounted, reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from '@/util/http'
import { parseTime } from '@/util/date'
export default defineComponent({
  name: 'notice',

  setup() {
    const router = useRouter()
    let title = ref('')
    let createPeople = ref('')
    let createTime = ref('')
    let status = ref('')

    //分页
    let currentPage = ref(1);
    let pageSize = ref(6);
    let totalData = ref();
    let total = ref();
    let background = ref(true)
    const noticeList = reactive({
      list: []
    })
    let play = ref([])
    onMounted(async () => {
      await axios({
        method: 'get', url: 'notice/findNoticeList', params: {
          currentPage: currentPage.value,
          pageSize: pageSize.value
        }
      })
        .then((res) => {
          total.value = res.data.data.data.total
          noticeList.list = res.data.data.data.list
          res.data.data.data.list.forEach(element => {
            if (element.status == '0') {
              element.play = ['编辑', '删除', '详情', '发布']
            }
            else {
              element.play = ['撤回', '详情']
            }
          });
        })
    })

    const change = () => {
      axios({
        method: 'get', url: 'notice/findNoticeList', params: {
          currentPage: currentPage.value,
          pageSize: pageSize.value
        }
      })
        .then((res) => {
          total.value = res.data.data.data.total
          noticeList.list = res.data.data.data.list
          res.data.data.data.list.forEach(element => {
            if (element.status == '0') {
              element.play = ['编辑', '删除', '详情', '发布']
            }
            else {
              element.play = ['撤回', '详情']
            }
          });
        })
    }

    const operate = (play, row) => {
      let rowId = row.noticeId
      if (play == '删除') {
        ElMessageBox.confirm(
          '是否确认删除该公告？',
          '删除确认',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        )
          .then(async () => {
            await axios({ method: 'post', url: '/notice/delNotice', params: { noticeId: rowId } }).then((res) => {
              change()
            })
            ElMessage({
              type: 'success',
              message: 'Delete completed',
            })
          })
          .catch(() => {
            ElMessage({
              type: 'info',
              message: 'Delete canceled',
            })
          })
      }
      else if (play == '撤回') {
        ElMessageBox.confirm(
          '是否确认撤回该公告？',
          '撤回确认',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        )
          .then(async () => {
            await axios({ method: 'post', url: '/notice/withdrawNotice', params: { noticeId: rowId } }).then((res) => {
              change()
            })
            ElMessage({
              type: 'success',
              message: 'Delete completed',
            })
          })
          .catch(() => {
            ElMessage({
              type: 'info',
              message: 'Delete canceled',
            })
          })
      }
      else if (play == '详情') {

        router.push(`/noticedetail?noticeId=${rowId}`)
      }
      else if (play == '编辑') {
        router.push(`/addnotice?noticeId=${rowId}`)
      }
      else if (play == '发布') {
        //yyyy-MM-dd
        axios({
          method: 'post', url: 'notice/releaseNotice', data: {
            noticeId: rowId, releaseDate: parseTime(new Date(), '{y}-{m}-{d}')
          }
        }).then((res) => {
          change()
        })
        router.push('/noticepubed')
      }
    }
    const check = async () => {
      if (status.value == '') {
        status.value = ''
      }
      else {
        status.value = status.value == '待发布' ? 0 : 1
      }
      await axios({
        method: 'get', url: 'notice/selectByTitle', params: {
          // createDate:createTime.value,
          // createPeople:createPeople.value,
          // noticeContent:'1',
          // noticeId:'1',
          // noticeTitle:title.value,
          // status:0,
          // 

          currentPage: currentPage.value,
          pageSize: pageSize.value,
          noticeTitle: title.value,
          createPeople: createPeople.value,
          createDate: createTime.value,
          status: status.value
        }
      }).then((res) => {
        noticeList.list = res.data.data.data.list
        res.data.data.data.list.forEach(element => {
          if (element.status == '0') {
            element.play = ['编辑', '删除', '详情', '发布']
          }
          else {
            element.play = ['撤回', '详情']
          }
        });
      })
    }
    const reset = async () => {
      title = ''
      createPeople = ''
      createTime = ''
      status = ''
      await axios({
        method: 'get', url: 'notice/findNoticeList', params: {
          currentPage: 1,
          pageSize: 10
        }
      }).then((res) => {
        noticeList.list = res.data.data.data.list
        res.data.data.data.list.forEach(element => {
          if (element.status == '0') {
            element.play = ['编辑', '删除', '详情', '发布']
          }
          else {
            element.play = ['撤回', '详情']
          }
        });
      })
    }

    const addnotice = () => {
      router.push('/addnotice')
    }
    const handleSizeChange = async (val) => {
      await axios({
        method: 'get', url: 'notice/findNoticeList', params: {
          currentPage: currentPage.value,
          pageSize: val
        }
      })
        .then((res) => {
          noticeList.list = res.data.data.data.list
          res.data.data.data.list.forEach(element => {
            if (element.status == '0') {
              element.play = ['编辑', '删除', '详情', '发布']
            }
            else {
              element.play = ['撤回', '详情']
            }
          });
        })
    }
    const handleCurrentChange = async (val) => {
      await axios({
        method: 'get', url: 'notice/findNoticeList', params: {
          currentPage: val,
          pageSize: pageSize.value
        }
      })
        .then((res) => {
          noticeList.list = res.data.data.data.list
          res.data.data.data.list.forEach(element => {
            if (element.status == '0') {
              element.play = ['编辑', '删除', '详情', '发布']
            }
            else {
              element.play = ['撤回', '详情']
            }
          });
        })
    }

    const pubed = ['编辑', '删除', '详情', '发布']
    const pub = ['撤回', '详情']
    for (let i = 0; i < noticeList.list.length; i++) {
      if (noticeList.list[i].status == '待发布') {
        noticeList.list[i].play = pubed
      }
      else {
        noticeList.list[i].play = pub
      }
    }
    return {
      operate, total, addnotice, title, createPeople, createTime, status, check, reset, ...toRefs(noticeList), play, currentPage, pageSize, parseTime, handleSizeChange, handleCurrentChange
    }
  }

})
</script>

<style scoped lang='less'>
.sel-all {
  box-sizing: border-box;
  width: 232px;
  height: 40px;
  padding: 2px 2px 2px 10px;
  font-family: '微软雅黑', sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 13px;
  letter-spacing: normal;
  text-align: left;
  text-transform: none;
  background-color: transparent;
  border-color: rgb(204, 204, 204);
  border-width: 1px;
  outline-color: rgb(204, 204, 204);
  border-radius: 5px;
}

.pagination {
  text-align: center;

  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      background-color: #409eff;
      color: #fff;
    }
  }
}

th {
  font-size: 13px !important;
  color: #333 !important;
  font-weight: normal !important;
}

.container-box {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
  color: #333;
  font-size: 13px;
}

.tableName {
  height: 45px;
  box-sizing: border-box;
}

.table-search {
  box-sizing: border-box;
  padding: 0px 40px;
  border-width: 0px;
  /* position: absolute;
    left: 0px;
    top: 0px; */
  width: 100%;
  height: 192px;
  background: inherit;
  background-color: rgb(255, 255, 255);
  border: none;
  border-radius: 4px;
  -moz-box-shadow: none;
  -webkit-box-shadow: none;
  box-shadow: none;
  font-family: 'Microsoft Tai Le Regular', 'Microsoft Tai Le', sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 14px;
  color: #666666;
  text-align: left;
  line-height: 18px;
}

.notice-list {
  box-sizing: border-box;
  padding: 20px 10px;
  margin-top: 10px;
  border-width: 0px;
  /* position: absolute;
    left: 0px;
    top: 0px; */
  width: 100%;
  // height: 100%;
  background: inherit;
  background-color: rgba(255, 255, 255, 1);
  border: none;
  border-radius: 4px;
  -moz-box-shadow: none;
  -webkit-box-shadow: none;
  box-shadow: none;
  font-family: 'Microsoft Tai Le Regular', 'Microsoft Tai Le', sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 14px;
  color: #666666;
  text-align: left;
  line-height: 18px;
}

input {
  box-sizing: border-box;
  width: 232px;
  height: 40px;
  padding: 2px 2px 2px 10px;
  font-family: '微软雅黑', sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 13px;
  letter-spacing: normal;
  color: #CCCCCC;
  text-align: left;
  text-transform: none;
  background-color: transparent;
  border-color: rgb(204, 204, 204);
  border-width: 1px;
  outline-color: rgb(204, 204, 204);
  border-radius: 5px;
}

.input-list {
  display: flex;
}

.input-box {
  box-sizing: border-box;
  padding-top: 20px;
  width: 330px;

}

label {
  color: #333;
  font-size: 13px;
  padding-right: 10px;
}

.search-btn-box {
  height: 100px;
  margin-top: 50px;
}

.search-btn {
  box-sizing: border-box;
  padding-top: 20px;
  display: flex;
}

.search-btn button {
  width: 92px;
  height: 40px;
  outline: none;
  border: 1px solid rgb(215, 215, 215);
  border-radius: 5px;
}

.new {
  height: 60px;
  color: #fff;
  font-size: 16px;
  line-height: 40px;
}

.new-notice {
  cursor: pointer;
  text-align: center;
  width: 150px;
  height: 40px;
  background-color: rgb(64, 158, 255);
  border-radius: 5px;
}

/deep/ .el-table thead {
  color: #333 !important;
  font-weight: normal !important;
}

/deep/ .el-table .cell {
  font-size: 13px;
  color: #333 !important;
  font-weight: normal !important;
  text-align: center;
}

/deep/.el-table .el-table__cell {
  padding: 11px 0;
  min-width: 0;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  z-index: 1;
}

.table-footer {
  height: 100px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>